package com.lazyframework.generator.web;

import com.lazyframework.commons.support.ResponseModel;
import com.lazyframework.commons.support.ResponseModels;
import com.lazyframework.generator.core.GenerateManager;
import com.lazyframework.generator.core.GenerateMetadata;
import com.lazyframework.generator.model.Column;
import com.lazyframework.generator.model.Schema;
import com.lazyframework.generator.model.Table;
import com.lazyframework.generator.service.SchemaService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这可是核心API啊，很难的！
 *
 * @author lazy
 */
@RestController
@AllArgsConstructor
public class IndexController {

    private final SchemaService schemaService;

    private final GenerateManager generateManager;

    @PostMapping("/generate")
    public ResponseModel<String> generate(@RequestBody GenerateMetadata metadata) {
        generateManager.generate(metadata);
        return ResponseModels.ok("Generate Success!");
    }

    @GetMapping("/schema/info")
    public ResponseModel<List<Schema>> schemas() {
        List<Schema> schemas = schemaService.schemas();
        return ResponseModels.ok(schemas);
    }

    @GetMapping("/schema/info/{schema}/tables")
    public ResponseModel<List<Table>> tables(@PathVariable String schema) {
        List<Table> schemas = schemaService.tables(schema);
        return ResponseModels.ok(schemas);
    }

    @GetMapping("/schema/info/{table}/columns")
    public ResponseModel<List<Column>> columns(@PathVariable String table, @RequestParam("schema") String schema) {
        List<Column> schemas = schemaService.columns(table, schema);
        return ResponseModels.ok(schemas);
    }

}
